#include "server.h"

void cutPath(const char *path, char (*dirs)[128]){
    //strtok会改变原来的字符串，所以要复制path到strPath
    char strPath[1024];
    strcpy(strPath,path);

    char* token=strtok(strPath,"/");
    strcpy(dirs[0],token);
    int i=1;
    while(1){
        token=strtok(NULL,"/");
        if(token==NULL){
            break;
        }
        strcpy(dirs[i++],token);
    }

}

void cutCmd(char *input,char *cmd,char *path1,char *path2) {
    char tmp[1024] = {0};
    char *p1, *p2, *p3;
    memcpy(tmp,input,strlen(input)-1);
    tmp[strlen(input)]='\0';
    p1 = strtok(tmp," ");
    memcpy(cmd, p1, strlen(p1) + 1);
    p2 = strtok(NULL," ");
    if(p2 == NULL){
       return;
    }
    memcpy(path1, p2, strlen(p2) + 1);
    p3 = strtok(NULL," ");
    if(p3 == NULL){
        return;
    }
    memcpy(path2, p3, strlen(p3) + 1);
}

int checkCmd(char *cmd) {
    if (strcmp(cmd,"cd") == 0 || strcmp(cmd, "ls") == 0 || strcmp(cmd, "gets") == 0 || strcmp(cmd, "puts") == 0 || strcmp(cmd, "remove") == 0 || strcmp(cmd, "rmdir") == 0 || strcmp(cmd, "mkdir") == 0 || strcmp(cmd, "pwd") == 0) {
        return 1;
    }
    return 0;
}

int cutTail(char *path, char *left, char *right) {
    
    char tmp[1024] = {0};
    memcpy(tmp, path, strlen(path));
    memcpy(left, path, strlen(path));
    char *p = strtok(tmp, "/");
    while (1) {

        char *tmp = strtok(NULL, "/");
        if (tmp == NULL) {
            memcpy(right, p, strlen(p) + 1);
            if (strlen(path) == strlen(right)) {
                strcpy(left, ".");
                break;
            }
            left[strlen(path) - strlen(right) - 1] = '\0';
            if (strlen(path) - strlen(right) - 1 == 0) {
                left[0] = '/';
            }
            break;
        }
        p = tmp;
    }
    return 0;
}

int changeDir_1(MYSQL* mysql,const char* username,char* curr_path,const char* dest_path){
    char sq[1024];//query字符串,保存mysql指令

    //cd 不接地址，直接回到根目录
    if(strlen(dest_path)==0){
        sprintf(curr_path,"/");
    }
    
    //cd 接. 无事发生
    if(strcmp(dest_path,".")==0){
        return 0;
    }

    //cd 接.. 回到上级目录(如果已经在根目录就什么都不做)
    if(strcmp(dest_path,"..")==0){
        if(strcmp(curr_path,"/")==0){
            return 0;
        }
        //找到当前目录的上级目录的id
        sprintf(sq,"select pre_id from tree where username = '%s' and path = '%s';",username,curr_path);
        int qret=mysql_query(mysql,sq);
        QUERY_CHECK(qret,mysql);
        MYSQL_RES *result=mysql_store_result(mysql);
        MYSQL_ROW row=mysql_fetch_row(result);
        //row[0]就是上级目录的id
        
        //找到上级目录的路径
        sprintf(sq,"select path from tree where id = '%s';",row[0]);
        qret=mysql_query(mysql,sq);
        QUERY_CHECK(qret,mysql);
        result=mysql_store_result(mysql);
        row=mysql_fetch_row(result);
        //row[0]就是path
        strcpy(curr_path,row[0]);
        return 0;
    }

    //不是.或..
    //找到当前所在目录在数据库中的id
    sprintf(sq,"select id from tree where username = '%s' and path = '%s';",username,curr_path);
    int qret=mysql_query(mysql,sq);
    QUERY_CHECK(qret,mysql);
    
    MYSQL_RES *result=mysql_store_result(mysql);
    MYSQL_ROW row=mysql_fetch_row(result);
    // row[0]就是当前目录的id

    // 找到pre_id = row[0] 的所有表项
    sprintf(sq,"select path from tree where pre_id = %s and tomb = 0 and md5 = '0';",row[0]);
    qret=mysql_query(mysql,sq);
    QUERY_CHECK(qret,mysql);

    result=mysql_store_result(mysql);
    char path[1024]={0};
    if(strcmp(curr_path,"/")==0){
        sprintf(path,"%s%s",curr_path,dest_path);
    }else{
        sprintf(path,"%s/%s",curr_path,dest_path);
    }
    while((row=mysql_fetch_row(result))!=NULL){
        //printf("path=%s,row[0]=%s\n",path,row[0]);
        if(strcmp(path,row[0])==0){
            strcpy(curr_path,row[0]);
            return 0;
        }
    }
    
    return -1;
}

int changeDir(MYSQL* mysql,const char* username,char* curr_path,const char* dest_path){
    if (strlen(dest_path) == 0) {
        strcpy(curr_path, "/");
        return 0;
    }
    char dest[1024]={0};
    strcpy(dest,dest_path);
    char *p=strtok(dest,"/");
    if(p){
        int ret=changeDir_1(mysql,username,curr_path,p);
        //printf("*p=%s,curr_path= %s\n",p,curr_path);
        if(ret==-1){
            return -1;
        }
    }
    while(1){
        p=strtok(NULL,"/");
        if(p){
            int ret=changeDir_1(mysql,username,curr_path,p);
            //printf("*p=%s,curr_path= %s\n",p,curr_path);
            if(ret==-1){
                return -1;
            }
        }else{
            break;
        }
    }
    return 0;
}


int changePath_1(MYSQL* mysql,const char* username,char* curr_path,const char* dest_path){
    char sq[1024];//query字符串,保存mysql指令

    //cd 不接地址，直接回到根目录
    if(strlen(dest_path)==0){
        sprintf(curr_path,"/");
    }
    
    //cd 接. 无事发生
    if(strcmp(dest_path,".")==0){
        return 0;
    }

    //cd 接.. 回到上级目录(如果已经在根目录就什么都不做)
    if(strcmp(dest_path,"..")==0){
        if(strcmp(curr_path,"/")==0){
            return 0;
        }
        //找到当前目录的上级目录的id
        sprintf(sq,"select pre_id from tree where username = '%s' and path = '%s';",username,curr_path);
        int qret=mysql_query(mysql,sq);
        QUERY_CHECK(qret,mysql);
        MYSQL_RES *result=mysql_store_result(mysql);
        MYSQL_ROW row=mysql_fetch_row(result);
        //row[0]就是上级目录的id
        
        //找到上级目录的路径
        sprintf(sq,"select path from tree where id = '%s';",row[0]);
        qret=mysql_query(mysql,sq);
        QUERY_CHECK(qret,mysql);
        result=mysql_store_result(mysql);
        row=mysql_fetch_row(result);
        //row[0]就是path
        strcpy(curr_path,row[0]);
        return 0;
    }

    //不是.或..
    //找到当前所在目录在数据库中的id
    sprintf(sq,"select id from tree where username = '%s' and path = '%s';",username,curr_path);
    int qret=mysql_query(mysql,sq);
    QUERY_CHECK(qret,mysql);
    
    MYSQL_RES *result=mysql_store_result(mysql);
    MYSQL_ROW row=mysql_fetch_row(result);
    // row[0]就是当前目录的id

    // 找到pre_id = row[0] 的所有表项
    sprintf(sq,"select path from tree where pre_id = %s and tomb = 0 ;",row[0]);
    qret=mysql_query(mysql,sq);
    QUERY_CHECK(qret,mysql);

    result=mysql_store_result(mysql);
    char path[1024]={0};
    if(strcmp(curr_path,"/")==0){
        sprintf(path,"%s%s",curr_path,dest_path);
    }else{
        sprintf(path,"%s/%s",curr_path,dest_path);
    }
    while((row=mysql_fetch_row(result))!=NULL){
        //printf("path=%s,row[0]=%s\n",path,row[0]);
        if(strcmp(path,row[0])==0){
            strcpy(curr_path,row[0]);
            return 0;
        }
    }
    
    return -1;
}

int listSearch(MYSQL* mysql,const char* username,const char* curr_path,char* list){
    //sq保存query语句
    char sq[1024]={0};
    sprintf(sq,"select id from tree where username = '%s' and path = '%s';\n",username,curr_path);
    int qret=mysql_query(mysql,sq);
    QUERY_CHECK(qret,mysql);
    MYSQL_RES *result=mysql_store_result(mysql);
    MYSQL_ROW row=mysql_fetch_row(result);
    //row[0]就是当前目录的id

    fprintf(stdout,"select name,md5,size,createtime from tree where pre_id = %s and tomb = 0;\n",row[0]);
    sprintf(sq,"select name,md5,size,createtime from tree where pre_id = %s and tomb = 0;",row[0]);
    qret=mysql_query(mysql,sq);
    QUERY_CHECK(qret,mysql);
    result=mysql_store_result(mysql);
    while((row=mysql_fetch_row(result))!=NULL){
        strcat(list, row[3]);
        strcat(list, "  ");
        strcat(list, row[2]);
        strcat(list, "\t");
        strcat(list, row[0]);
        if (strcmp(row[1], "0") == 0) {
            strcat(list, "/");
        } 
        strcat(list,"\n");
    }
    //sprintf(sq,"select name,size,createtime from tree where pre_id = %s and tomb = 0;");

    return 0;
}


int changePath(MYSQL* mysql,const char* username,char* curr_path,const char* dest_path){
    
    if (strlen(dest_path) == 0) {
        strcpy(curr_path, "/");
        return 0;
    }
    char dest[1024]={0};
    strcpy(dest,dest_path);
    char *p=strtok(dest,"/");
    if(p){
        int ret=changePath_1(mysql,username,curr_path,p);
        //printf("*p=%s,curr_path= %s\n",p,curr_path);
        if(ret==-1){
            return -1;
        }
    }
    while(1){
        p=strtok(NULL,"/");
        if(p){
            int ret=changePath_1(mysql,username,curr_path,p);
            //printf("*p=%s,curr_path= %s\n",p,curr_path);
            if(ret==-1){
                return -1;
            }
        }else{
            break;
        }
    }
    return 0;
}

